using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
using System.Xml;
using System.Collections;
using System.IO;
using System.Security.Cryptography.X509Certificates;


namespace edu.stanford.hci.ideas
{
    public class MyPictureBox : Control
    {
        readonly ImageAttributes attr = new ImageAttributes();

        // Initialize the color matrix. Note the alpha value in row 4, column 4.
        float[][] matrixItems = {
new float[] {1, 0, 0, 0, 0}, // R
new float[] {0, 1, 0, 0, 0}, // G
new float[] {0, 0, 1, 0, 0}, // B
new float[] {0, 0, 0, 0.5f, 0}, // A
new float[] {0, 0, 0, 0, 1}}; // W (?)

        ColorMatrix colorMatrix;

        public Image Image;

        public MyPictureBox(Image img)
        {
            Image = img;
            colorMatrix = new ColorMatrix(matrixItems);
            //SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //BackColor = Color.White;

            SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.SupportsTransparentBackColor, true);
        }

        public void setAlpha(float alpha)
        {
            colorMatrix.Matrix33 = alpha;
        }

        public float getAlpha()
        {
            return colorMatrix.Matrix33;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //Console.WriteLine("painting with alpha " + colorMatrix.Matrix33);
            // Create an ImageAttributes object and set its color matrix.
            attr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            if (Image != null)
                e.Graphics.DrawImage(Image, new Rectangle(0, 0, Image.Width, Image.Height), 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, attr);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
        }

    }
}
